3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\Block\DatabaseBlock
;
26 * API module that facilitates the blocking of users. Requires API write mode
31 class ApiBlock
extends ApiBase
{
33 use ApiBlockInfoTrait
;
36 * Blocks the user specified in the parameters for the given expiry, with the
37 * given reason, and with all other settings provided in the params. If the block
38 * succeeds, produces a result containing the details of the block and notice
39 * of success. If it fails, the result will specify the nature of the error.
41 public function execute() {
42 $this->checkUserRightsAny( 'block' );
44 $user = $this->getUser();
45 $params = $this->extractRequestParams();
47 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
49 # T17810: blocked admins should have limited access here
50 $block = $user->getBlock();
52 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
53 if ( $status !== true ) {
57 [ 'blockinfo' => $this->getBlockDetails( $block ) ]
62 $editingRestriction = 'sitewide';
63 $pageRestrictions = '';
64 $namespaceRestrictions = '';
65 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
66 if ( $params['partial'] ) {
67 $editingRestriction = 'partial';
70 $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
71 $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
74 if ( $params['userid'] !== null ) {
75 $username = User
::whoIs( $params['userid'] );
77 if ( $username === false ) {
78 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
80 $params['user'] = $username;
83 list( $target, $type ) = SpecialBlock
::getTargetAndType( $params['user'] );
85 // T40633 - if the target is a user (not an IP address), but it
86 // doesn't exist or is unusable, error.
87 if ( $type === DatabaseBlock
::TYPE_USER
&&
88 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $params['user'] ) )
90 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
94 if ( $params['tags'] ) {
95 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
96 if ( !$ableToTag->isOK() ) {
97 $this->dieStatus( $ableToTag );
101 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
102 $this->dieWithError( 'apierror-canthide' );
104 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
105 $this->dieWithError( 'apierror-cantblock-email' );
109 'PreviousTarget' => $params['user'],
110 'Target' => $params['user'],
116 'Expiry' => $params['expiry'],
117 'HardBlock' => !$params['anononly'],
118 'CreateAccount' => $params['nocreate'],
119 'AutoBlock' => $params['autoblock'],
120 'DisableEmail' => $params['noemail'],
121 'HideUser' => $params['hidename'],
122 'DisableUTEdit' => !$params['allowusertalk'],
123 'Reblock' => $params['reblock'],
124 'Watch' => $params['watchuser'],
126 'Tags' => $params['tags'],
127 'EditingRestriction' => $editingRestriction,
128 'PageRestrictions' => $pageRestrictions,
129 'NamespaceRestrictions' => $namespaceRestrictions,
132 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
133 if ( $retval !== true ) {
134 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
137 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
138 $res['user'] = $params['user'];
139 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
141 $block = DatabaseBlock
::newFromTarget( $target, null, true );
142 if ( $block instanceof DatabaseBlock
) {
143 $res['expiry'] = ApiResult
::formatExpiry( $block->getExpiry(), 'infinite' );
144 $res['id'] = $block->getId();
146 # should be unreachable
147 $res['expiry'] = ''; // @codeCoverageIgnore
148 $res['id'] = ''; // @codeCoverageIgnore
151 $res['reason'] = $params['reason'];
152 $res['anononly'] = $params['anononly'];
153 $res['nocreate'] = $params['nocreate'];
154 $res['autoblock'] = $params['autoblock'];
155 $res['noemail'] = $params['noemail'];
156 $res['hidename'] = $params['hidename'];
157 $res['allowusertalk'] = $params['allowusertalk'];
158 $res['watchuser'] = $params['watchuser'];
160 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
161 $res['partial'] = $params['partial'];
162 $res['pagerestrictions'] = $params['pagerestrictions'];
163 $res['namespacerestrictions'] = $params['namespacerestrictions'];
166 $this->getResult()->addValue( null, $this->getModuleName(), $res );
169 public function mustBePosted() {
173 public function isWriteMode() {
177 public function getAllowedParams() {
180 ApiBase
::PARAM_TYPE
=> 'user',
183 ApiBase
::PARAM_TYPE
=> 'integer',
189 'autoblock' => false,
192 'allowusertalk' => false,
194 'watchuser' => false,
196 ApiBase
::PARAM_TYPE
=> 'tags',
197 ApiBase
::PARAM_ISMULTI
=> true,
201 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
202 $params['partial'] = false;
203 $params['pagerestrictions'] = [
204 ApiBase
::PARAM_ISMULTI
=> true,
205 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 10,
206 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 10,
208 $params['namespacerestrictions'] = [
209 ApiBase
::PARAM_ISMULTI
=> true,
210 ApiBase
::PARAM_TYPE
=> 'namespace',
217 public function needsToken() {
221 protected function getExamplesMessages() {
222 // phpcs:disable Generic.Files.LineLength
224 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
225 => 'apihelp-block-example-ip-simple',
226 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
227 => 'apihelp-block-example-user-complex',
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';